package com.nCoV.EpidemicSurveillance.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import com.nCoV.EpidemicSurveillance.common.utils.DateUtil;
import com.nCoV.EpidemicSurveillance.common.utils.ListUtil;
import com.nCoV.EpidemicSurveillance.common.utils.MapUtil;
import com.nCoV.EpidemicSurveillance.domain.Person;
import com.nCoV.EpidemicSurveillance.repository.AreaRepository;
import com.nCoV.EpidemicSurveillance.repository.PersonRepository;
import com.nCoV.EpidemicSurveillance.repository.TongJiRepository;
import com.nCoV.EpidemicSurveillance.repository.TrafficRepository;

/**
 * @author 罗穆健
 * @date 2020年4月14日
 */
@Service
public class DataSearchService {
	
	@Autowired
	PersonRepository personRepository;
	
	@Autowired
	TongJiRepository tongJiRepository;
	
	@Autowired
	AreaRepository areaRepository;
	
	@Autowired
	TrafficRepository trafficRepository;
	
	Logger logger;

	/**
	 * 
	 * @author 罗穆健
	 * @date 2020年4月26日
	 * @param name
	 * @param idCard
	 * @return 1正常 2身份证存在 3数据库操作失败
	 */
	public Map<String, Object> getUserMsg(String name, String idCard) {
		// TODO Auto-generated method stub
		Map<String, Object> personMap = new HashMap<String, Object>();
		try {
			Person person =  personRepository.createPerson(name,idCard);
			personMap = MapUtil.objectToMap(person);
			personMap.put("code","1");
		} catch (Exception e) {
			// TODO: handle exception
			if(e instanceof DataIntegrityViolationException) {

				personMap.put("code","2");
			}else {

				personMap.put("code","3");
			}
		}
		return personMap;
	}

	public List<Map<String, Object>> getContactorMsg(String idCard) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> contactList = new ArrayList<Map<String,Object>>();
		Map<String, Object> codeMap = new HashMap<String, Object>();
		try {
			contactList = personRepository.getZdjc(idCard);
			for (Map<String, Object> map : contactList) {
				map.put("contactType","主动接触");
			}
			List<Map<String, Object>> contactList1 = personRepository.getBdjc(idCard);
			for (Map<String, Object> map : contactList1) {
				map.put("contactType","被动接触");
			}

			contactList.addAll(contactList1);
			ListUtil.ListSort(contactList, "contactTime");
			codeMap.put("code", "1");
			
		} catch (Exception e) {
			// TODO: handle exception
			codeMap.put("code", "2");
		}
		contactList.add(codeMap);
		return contactList;
	}

	public Map<String, Object> updateUserMsg(Person person) {
		// TODO Auto-generated method stub
		Map<String, Object> codeMap = new HashMap<String, Object>();
		try {
			personRepository.updateUserMsg(person);
			codeMap.put("code", "1");
		} catch (Exception e) {
			// TODO: handle exception
			codeMap.put("code", "2");
		}
		return codeMap;
	}

	public List<Map<String, Object>> getTj() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> tjList = new ArrayList<Map<String,Object>>();
		try {
			tjList = tongJiRepository.getAllTongJi();
			ListUtil.ListSortDay(tjList, "time");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return tjList;
	}

	public List<Map<String, Object>> getAreaData(String address, String startTime, String endTime) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> areaDataList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> returnAreaData = new ArrayList<Map<String,Object>>();
		try {
			address = ".*" + address + ".*";
			areaDataList = areaRepository.getAreaData(address);
			for (Map<String, Object> map : areaDataList) {
				if("".equals(startTime) && "".equals(endTime)) {
					returnAreaData.add(map);
				}else if("".equals(endTime)) {
					if(DateUtil.compareDate(startTime, map.get("reachTime").toString())) {
						returnAreaData.add(map);
					}
				}else if("".equals(startTime)) {
					if(!DateUtil.compareDate(endTime, map.get("leaveTime").toString())) {
						returnAreaData.add(map);
					}
				}else {
					if(DateUtil.compareDate(startTime, map.get("reachTime").toString()) &&
							!DateUtil.compareDate(endTime, map.get("leaveTime").toString())) {
						returnAreaData.add(map);
					}
				}
			}
			ListUtil.ListSort(returnAreaData, "reachTime");
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(e);
		}
		return returnAreaData;
	}

	public List<Map<String, Object>> getTrafficData(String trainNumber, int skipNum, int limitNum) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> trafficList = new ArrayList<Map<String,Object>>();
		Map<String, Object> codeMap = new HashMap<String, Object>();
		try {
			trainNumber = ".*" + trainNumber + ".*";
			trafficList = trafficRepository.getTrafficData(trainNumber,skipNum,limitNum);
			int count = trafficRepository.getTrafficCount(trainNumber);
			ListUtil.ListSort(trafficList, "startTime");
			codeMap.put("code", "1");
			codeMap.put("trafficCount",count);
			System.out.println(trafficList);
		} catch (Exception e) {
			// TODO: handle exception
			codeMap.put("code","2");
			logger.info(e);
		}
		trafficList.add(codeMap);
		return trafficList;
	}

}
